-- Mountline Panel
local PANEL = {}

--AccessorFunc( PANEL, "m_MasterName", 			"MasterName" )
--AccessorFunc( PANEL, "m_Text", 					"Text" )


function PANEL:Init()
	self.clabel = vgui.Create( "DCheckBoxLabel", self )
	self.clabel:SetIndent( 16 )
	
	self.mount_button = vgui.Create( "DImageButton", self )
	self.mount_button:SetImage( "gui/silkicons/box" )	
	
	self.menu_button = vgui.Create( "DImageButton", self )
	self.menu_button:SetImage( "gui/silkicons/application_view_tile" )	
	self.menu_button:SetTooltip( "Menu" )
	
	self.barnstar = vgui.Create( "InsStyleCheckBox", self )
	self.barnstar:SetEnabledTexture( "gui/silkicons/star" )	
	self.barnstar:SetDisabledTexture( "inscript/dot" )	
	self.barnstar:SetTooltip( "Favorite!" )
end


function PANEL:SetMasterName( sName )
	if not sName or not table.HasValue( inscript_plugin.GetNamesTable() , sName ) then return end
	self.mastername = sName
	
	local plugin = inscript_plugin.GetPluginObject( self.mastername )
	self.clabel:SetText( plugin:GetDisplayName() )
	self.clabel:SetConVar( "inscript_plugin_" .. self.mastername )
	
	--Barnstar Convar
	self.barnstar:SetConVar( "inscript_barnstar_" .. self.mastername )
	
	self:UpdateLabel( )
	--self.menu_button:SetEnabled( seemsEnabled )
	
	self.mount_button.DoClick = function()
		if not self.mastername or not table.HasValue( inscript_plugin.GetNamesTable() , self.mastername ) then return end
		
		local plugin = inscript_plugin.GetPluginObject( self.mastername )
		local isEnabled = plugin:IsMounted()
		
		if not isEnabled then plugin:Mount()
		else 				  plugin:Unmount() end
		
		if not plugin:IsPerform() then
			self:UpdateLabel( )
			--self.mount_button:SetText( (not isEnabled) and "Unmount" or "Mount" )
			--self.menu_button:SetEnabled( (not isEnabled) )
			
		end
		
	end
	
	self.menu_button.DoClick = function()
		if not self.mastername or not table.HasValue( inscript_plugin.GetNamesTable() , self.mastername ) then return end
		
		local plugin = inscript_plugin.GetPluginObject( self.mastername )
		plugin:ShowMenu()
		
	end
	
end

function PANEL:UpdateLabel( )
	local plugin = inscript_plugin.GetPluginObject( self.mastername )
	local isPerform = plugin:IsPerform()
	local seemsEnabled = isPerform or plugin:IsMounted()
	--self.mount_button:SetText( isPerform and "Perform" or (seemsEnabled and "Unmount" or "Mount") )
	if isPerform then
		self.mount_button:SetImage( "gui/silkicons/arrow_refresh" )
		self.mount_button:SetTooltip( "Perform" )
		
	elseif seemsEnabled then
		self.mount_button:SetImage( "gui/silkicons/check_on" )
		self.mount_button:SetTooltip( "Unmount" )
		
	else
		self.mount_button:SetImage( "gui/silkicons/check_off" )
		self.mount_button:SetTooltip( "Mount" )
		
	end
	--self.menu_button:SetEnabled( isEnabled )
	
end


function PANEL:PerformLayout()
	self:SetTall( 18 )
	--self.mount_button:SetTall( 18 )
	--self.mount_button:SetWide( 64 )
	self.mount_button:SetTall( 16 )
	self.mount_button:SetWide( 16 )
	self.mount_button:CenterVertical()
	
	--self.menu_button:SetTall( 18 )
	--self.menu_button:SetWide( 48 )
	self.menu_button:SetTall( 16 )
	self.menu_button:SetWide( 16 )
	self.menu_button:CenterVertical()
	
	self.clabel:SizeToContents()
	self.clabel:AlignLeft( 5 )
	self.clabel:CenterVertical()
	
	self.barnstar:SetTall( 16 )
	self.barnstar:SetWide( 16 )
	self.barnstar:AlignLeft( 3 )
	self.barnstar:CenterVertical()
	
	--self.clabel:AlignTop( 0 )
	--self.mount_button:AlignTop( 0 )
	--self.menu_button:AlignTop( 0 )
	
	
	local x,y = self.mount_button:GetSize()
	self.menu_button:AlignRight( 3 + x + 3 )
	
	self.mount_button:AlignRight( 3 )

end



vgui.Register( "InsMount", PANEL, "DPanel" )